package capricornus.components.utils
{
	import capricornus.core.StageManager;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	public class MovieClipUtils
	{
		private static var _currentFrame:int = 1;
		public function MovieClipUtils()
		{
		}
		
		public static function stopMC( mc:Sprite ):void
		{
			if( mc==null ) return;
			if( mc is MovieClip ) (mc as MovieClip).stop();
			for( var i:int=0; i<mc.numChildren; i++ )
			{
				var m:Sprite = mc.getChildAt( i ) as Sprite;
				if( m != null )
				{
					stopMC( m );
				}
			}
		}
		
		public static function removeMC( mc:Sprite ):void
		{
			if( mc==null ) return;
			if( mc is MovieClip ) (mc as MovieClip).stop();
			for( var i:int=0; i<mc.numChildren; i++ )
			{
				var m:DisplayObject = mc.removeChildAt(0);
				if( m is Bitmap )
				{
					(m as Bitmap).bitmapData = null;
				}
				else if( m is Loader )
				{
					(m as Loader).unload();
				}
				else if(m is Sprite )
				{
					removeMC( m as Sprite );
				}
			}
		}
		
		public static function playMC( mc:Sprite ):void
		{
			if( mc==null ) return;
			if( mc is MovieClip ) (mc as MovieClip).play();
			for( var i:int=0; i<mc.numChildren; i++ )
			{
				var m:Sprite = mc.getChildAt( i ) as Sprite;
				if( m != null )
				{
					playMC( m );
				}
			}
		}
		
		private static var _arrCallFun:Array = new Array();
		public static function callLater( func:Function, data:Object=null ):void
		{
			_arrCallFun.push( [_currentFrame+1, func, data] );
			if( false==_isDetectRunning )
			{
				_isDetectRunning = true;
				StageManager.stage.addEventListener( Event.ENTER_FRAME, onDetectEnterFrame, false, 0, true );
			}
		}

		private static var _isDetectRunning:Boolean = false;
		private static var _arrDetectMCs:Array = new Array();
		public static function detectMCStop( mc:MovieClip, stopCallbackFun:Function ):void
		{
			_arrDetectMCs.push( [mc, stopCallbackFun] );
			if( false==_isDetectRunning )
			{
				_isDetectRunning = true;
				StageManager.stage.addEventListener( Event.ENTER_FRAME, onDetectEnterFrame, false, 0, true );
			}
		}
		
		private static function onDetectEnterFrame( e:Event ):void
		{
			var i:int;
			_currentFrame++;
			var arr:Array;
			for( i=0; i<_arrDetectMCs.length; )
			{
				arr = _arrDetectMCs[i];
				var mc:MovieClip = arr[0];
				if( mc.currentFrame==mc.totalFrames )
				{
					var fun:Function = arr[1];
					if( Boolean(fun) )
					{
						try
						{
							fun( mc );
						}
						catch( err:Error ){};
						
						try
						{
							fun();
						}
						catch( er:Error ){};
					}
					_arrDetectMCs.splice( i, 1 );
					arr.pop();
					arr.pop();
				}
				else
				{
					i++;
				}
			}
			for( i=0; i<_arrCallFun.length; )
			{
				arr = _arrCallFun[i];
				if( arr[0]==_currentFrame )
				{
					if( arr[2] )
					{
						arr[1]( arr[2] );						
					}
					else
					{
						arr[1]();
					}
					arr.splice( i, 1 );
					arr.pop();
					arr.pop();
					arr.pop();
				}
				else
				{
					i++;
				}
			}
				
			if( _arrDetectMCs.length==0 &&_arrCallFun.length==0 )
			{
				_isDetectRunning = false;
				StageManager.stage.removeEventListener( Event.ENTER_FRAME, onDetectEnterFrame );
			}
		}
	}
}